package cn.jdy.plugins.admin;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.NginxPurgeCacheUtil;
import cn.jdy.plugins.entity.RedPacketReceive;
import cn.jdy.plugins.service.RedPacketReceiveService;

/**
 * @title 红包提现控制类
 * @author lutan
 */
@Controller
@RequestMapping("/redPacketReceive")
public class RedPacketReceiveCtrl extends BaseCtrl {
	@Autowired
	private RedPacketReceiveService redPacketReceiveService;

	// 默认列表路径
	private static String viewName = "redirect:/RedPacketReceive/list?s=";

	/**
	 * 初始化
	 * 
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(required = false) Integer s) {
		ModelAndView mv = new ModelAndView();
		if (s != null)
			switch (s) {
			case 1:
				mv.addObject("msg", "保存成功！");
				break;
			case 2:
				mv.addObject("msg", "更新成功！");
				break;
			case 3:
				mv.addObject("msg", "删除成功！");
				break;
			case 4:
				mv.addObject("msg", "操作失败！");
				break;
			default:
				break;
			}
		mv.setViewName("/operate/redPacketReceiveList.jsp");
		return mv;
	}

	/**
	 * 异步查询
	 * 
	 * @param RedPacketReceive
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(RedPacketReceive redPacketReceive,String fromDate, String toDate) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {

			jsonMap.put("dataList",
					redPacketReceiveService.query(redPacketReceive,fromDate, toDate));
			jsonMap.put("iTotalDisplayRecords",
					redPacketReceiveService.count(redPacketReceive));
			jsonMap.put("iTotalRecords",
					redPacketReceiveService.count(redPacketReceive));
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return jsonMap;
	}

	/**
	 * 新增
	 * 
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit() {
		return edit(null);
	}

	/**
	 * 编辑
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView(
				"/operate/redPacketReceiveDetail.jsp");
		   
		mv.addObject("data", id == null || id == 0 ? id
				: this.redPacketReceiveService.find(id));
		return mv;
	}

	/**
	 * 保存（更新）红包提现
	 * 
	 * @param RedPacketReceive
	 * @return
	 */
	@SuppressWarnings("static-access")
	@RequestMapping("/save")
	public ModelAndView save(RedPacketReceive redPacketReceive) {
		ModelAndView mv = new ModelAndView();
		try {
			if (redPacketReceive.getId() == null) {
				redPacketReceive.setCreateTime(DateUtil
						.getCurrentDate(DateUtil._DEFAULT2));
				this.redPacketReceiveService.save(redPacketReceive);
				mv.setViewName(this.viewName.concat("1"));
			} else {
				redPacketReceive.setUpdateTime(DateUtil
						.getCurrentDate(DateUtil._DEFAULT2));
				this.redPacketReceiveService.update(redPacketReceive);
				mv.setViewName(this.viewName.concat("2"));
				// 清理内容页缓存
				NginxPurgeCacheUtil.purge(6, redPacketReceive.getId());
			}
			// 清理内容页缓存
			NginxPurgeCacheUtil.purge(5, redPacketReceive.getId());
		} catch (Exception e) {
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "红包提现保存异常", e);
		}
		return mv;
	}

	/**
	 * 删除红包提现
	 * 
	 * @return
	 */
	@SuppressWarnings("static-access")
	@RequestMapping("/del/{id}")
	public ModelAndView del(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView();
		try {
			this.redPacketReceiveService.delete(id);
			mv.setViewName(this.viewName.concat("3"));
			// 清理内容页缓存
			NginxPurgeCacheUtil.purge(6, id);
		} catch (Exception e) {
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "删除红包提现异常！", e);
		}
		return mv;
	}
	
	/**
	 * 状态切换
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping("/statusSwitch")
	@ResponseBody
	public Map<String, Object> statusSwitch(Long id, String status) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			RedPacketReceive redPacketReceive = redPacketReceiveService.find(id);
			if (redPacketReceive != null) {
//				redPacketReceive.setRpStatus(status);
				redPacketReceive.setUpdateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				redPacketReceiveService.update(redPacketReceive);
				jsonMap.put("ok", "");
			} else {
			}
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "状态切换异常", e);
		}
		return jsonMap;
	}
}
